//
//  PubFun.js
//  SugLisSystem
//	常用方法集合
//  Created by MrRight on 2012-11-21.
//  Copyright 2012 MrRight. All rights reserved.
//
/**
 * 判断是否为空或则是否为定义
 * @param 需要判断的参数
 * @return 是否为空
 * */
function isNull(param) {
	return ( typeof param == 'undefined' || param == null);
}

exports.isNull = isNull;
/**
 * 通过起始日期和终止日期计算以时间间隔单位为计量标准的时间间隔
 * @param startDate 起始日期,格式为："YYYY-MM-DD"
 * @param endDate 终止日期,格式为："YYYY-MM-DD"
 * @param unit 时间间隔单位,可用值("Y"--年 "M"--月 "D"--日)
 * @return 时间间隔
 */
function calInterval(startDate, endDate, unit) {
	//将YYYY-MM-DD的时间格式，转换为 YYYY/MM/DD的格式
	startDate = startDate.replace(/-/g, "/");
	endDate = endDate.replace(/-/g, "/");
	//开始时间
	var sTime = new Date(startDate);
	//结束时间
	var eTime = new Date(endDate);
	var interval = new Object();
	//开始时间参数
	var sYears = sTime.getFullYear();
	var sMonths = sTime.getMonth();
	var sDays = sTime.getDay();
	//结束时间参数
	var eYears = eTime.getFullYear();
	var eMonths = eTime.getMonth();
	var eDays = eTime.getDay();

	//初步计算
	interval['Y'] = eYears - sYears;
	interval['M'] = interval['Y'] * 12 + eMonths - sMonths;

	//计算年
	var tempsTime = new Date(eYears, sMonths, sDays);
	if (eTime - tempsTime < 0) {
		interval['Y']--;
	}

	//计算月
	tempsTime = new Date(eYears, eMonths, sDays);
	if (eTime - tempsTime < 0) {
		interval['M']--;
	}

	//计算天
	interval['D'] = Math.ceil((eTime - sTime) / 86400000);

	return interval[unit];
}

exports.calInterval = calInterval;
/**
 * 比较获取两天中较早的一天
 * @param oneDate 起始日期,格式为："YYYY-MM-DD"
 * @param anotherDate 终止日期,格式为："YYYY-MM-DD"
 * @return 时间间隔
 */
function getBeforeDate(oneDate, anotherDate) {
	//将YYYY-MM-DD的时间格式，转换为 YYYY/MM/DD的格式
	var temponeDate = oneDate.replace(/-/g, "/");
	var tempanotherDate = anotherDate.replace(/-/g, "/");
	return new Date(temponeDate) - new Date(tempanotherDate) > 0 ? anotherDate : oneDate;
}

exports.getBeforeDate = getBeforeDate;
/**
 * 比较获取两天中较晚的一天
 * @param oneDate 起始日期,格式为："YYYY-MM-DD"
 * @param anotherDate 终止日期,格式为："YYYY-MM-DD"
 * @return 时间间隔
 */
function getLaterDate(oneDate, anotherDate) {
	//将YYYY-MM-DD的时间格式，转换为 YYYY/MM/DD的格式
	var temponeDate = oneDate.replace(/-/g, "/");
	var tempanotherDate = anotherDate.replace(/-/g, "/");
	return new Date(temponeDate) - new Date(tempanotherDate) > 0 ? oneDate : anotherDate;
}

exports.getLaterDate = getLaterDate;

function getCurrentDate() {
	var currentDate = new Date();
	var cYear = currentDate.getFullYear();
	var cMonth = currentDate.getMonth() + 1;
	if (cMonth < 10) {
		cMonth = '0' + cMonth;
	}
	var cDay = currentDate.getDate();
	if (cDay < 10) {
		cDay = '0' + cDay;
	}
	return cYear + '-' + cMonth + '-' + cDay;
}

exports.getCurrentDate = getCurrentDate;

function getCurrentTime() {
	var currentDate = new Date();
	var cHour = currentDate.getHours();
	if (cHour < 10) {
		cHour = '0' + cHour;
	}
	var cMinute = currentDate.getMinutes();
	if (cMinute < 10) {
		cMinute = '0' + cMinute;
	}
	var cSecond = currentDate.getSeconds();
	if (cSecond < 10) {
		cSecond = '0' + cSecond;
	}
	return cHour + ':' + cMinute + ':' + cSecond;
}

exports.getCurrentTime = getCurrentTime;

/**
 * 将字符串补数,将sourString的<br>后面</br>用cChar补足cLen长度的字符串,如果字符串超长，则不做处理
 * <p><b>Example: </b><p>
 * <p>RCh("Minim", "0", 10) returns "Minim00000"<p>
 * @param sourString 源字符串
 * @param cChar 补数用的字符
 * @param cLen 字符串的目标长度
 * @return 字符串
 */
function RCh(sourString, cChar, cLen) {
	var tLen = sourString.length();
	for (var i = 0; i < cLen - tLen; i++) {
		sourString += cChar
	}
	return sourString;
}

exports.RCh = RCh;
/**
 * 将字符串补数,将sourString的<br>前面</br>用cChar补足cLen长度的字符串,如果字符串超长，则不做处理
 * <p><b>Example: </b><p>
 * <p>LCh("Minim", "0", 10) returns "00000Minim"<p>
 * @param sourString 源字符串
 * @param cChar 补数用的字符
 * @param cLen 字符串的目标长度
 * @return 字符串
 */
function LCh(sourString, cChar, cLen) {
	var tLen = sourString.length();
	for (var i = 0; i < cLen - tLen; i++) {
		sourString = cChar + sourString
	}
	return sourString;
}

exports.LCh = LCh;

function formatNumberByThous(number) {
	if ( typeof number !== 'string') {
		number = number.toString();
	}
	var re = /\d{1,3}(?=(\d{3})+$)/g;
	var str = number.replace(/^(\d+)((\.\d+)?)$/, function(s, s1, s2) {
		return s1.replace(re, "$&,") + s2;
	});

	return str;
}

exports.formatNumberByThous = formatNumberByThous;

/**
 * 保留小数位数
 * @param number 需要保留的数
 * @param digit 需要保留的小数位数
 * */
function round(numbers, digit) {
	var temp = Math.pow(10, digit);
	var fixNum = Math.pow(10, 0 - (digit + 1));
	var fixNumbers = Math.round(numbers * temp) / temp + fixNum;
	return fixNumbers.toFixed(digit);
}

exports.round = round
